﻿using System.Collections.Generic;
using System.Linq;
using MaxStoreErp.Core.Extensions;
using MaxStoreShop.Domain.Shop;
using MaxStoreShop.Logic.RepositoriesContracts.Shop;

namespace MaxStoreShop.Dal.Repositories.Shop
{
    public class DebtorRuleRepository : RepositoryBase, IDebtorRuleRepository
    {
        public IEnumerable<DebtorRule> GetAllRules()
        {
            return Context.DebtorRules.AsNoTracking().NotDeleted();
        }

        public void DeleteRule(int debtorRuleId)
        {
            SafeDelete(Context.DebtorRules.First(c=>c.Id == debtorRuleId));
        }

        public void SaveRule(DebtorRule debtorRule)
        {
            debtorRule.Character = Context.Characters.First(c => c.Id == debtorRule.Character.Id);
            debtorRule.CharacterId = debtorRule.Character.Id;
            SaveEntity(debtorRule);
        }

        public DebtorRule GetCharacterDebtRule(int characterId)
        {
            return Context.DebtorRules.NotDeleted().FirstOrDefault(c => c.Character.Id == characterId);
        }
    }
}
